package ie.dit.reg08.manager;

import java.util.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Domain {

	Vector<Entity> vEntities;
		
	public Domain(Node n) {
		vEntities = new Vector<Entity>();
		if (n.hasChildNodes()) {
			NodeList nl = n.getChildNodes();
			int num = nl.getLength();
			for (int i = 0; i < num; i++) {
				if(nl.item(i).getNodeName().equals("ENTITY")) {
					Entity newEntity = new Entity(nl.item(i));
					vEntities.add(newEntity);
				}
				
			}
		} else {
			System.err.println("domain: n has no children!");
		}
	}
		
	
	public Vector<Entity> distractorSet(Hashtable<String, String> description) {
		Vector<Entity> vDistractors = new Vector<Entity>();
		Iterator<Entity> i = vEntities.iterator();
		Entity e;
		while (i.hasNext()) {
			e = i.next();
			if(!e.isTarget()) {
				if(e.fulfills(description)) {
					vDistractors.add(e);
				}
			}
		}
		return vDistractors;
	}
	
	public void printDomain() {
		System.out.println("Domain {");
		Iterator<Entity> i = vEntities.iterator();
		Entity fe;
		while(i.hasNext()) {
			fe = i.next();
			System.out.println(fe.toString());
		}
		System.out.println("} End Domain;");
	}
	
	public String getXMLDomainDescription() {
		String s = "<DOMAIN>\n";
		Iterator<Entity> i = vEntities.iterator();
		while(i.hasNext()) {
			s = s + i.next().getXMLEntityDescription();
		}
		
		s = s + "</DOMAIN>\n";
		return s;
	}
	
	public boolean isPlural() {
		int targets = 0;
		
		Iterator<Entity> i = vEntities.iterator();
		while(i.hasNext()) {
			if((i.next()).isTarget()) {
				targets++;
			}
		}
		
		if (targets > 1)
			return true;
		return false;
	}
	
	public Entity getTarget() {
		Iterator<Entity> i = vEntities.iterator();
		Entity e;
		while(i.hasNext()) {
			e = i.next();
			if(e.isTarget()) {
				return e;
			}
		}
		return null;
	}
	
	
}
